//
//  TishiDynamicDetailVC.m
//  TuYouAPP
//
//  Created by L on 14-3-14.
//
//

#import "TishiDynamicDetailVC.h"
#import "DynamicDetailVC.h"
#import "JinghuaDynamicDetailVC.h"
#import "DynamicDetailCell.h"
#import "DataModel.h"
#import "DynamicReplyVC.h"
#import "PublishVC.h"
#import "YanTuVC.h"
#import "UIImageView+WebCache.h"
#import "Util.h"
#import "UIButton+WebCache.h"
#import "MyPublishVC.h"
#import "LoginVC.h"
@interface TishiDynamicDetailVC ()
{
    NSMutableArray *tempArray;
    UIButton *tempBtn;
    TYUserState *topstate;
    NSDate *lastTime;
    NSDate *firstTime;
    BOOL nowIsNew;
}
@end

@implementation TishiDynamicDetailVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.Title = @"重要提示";
}

-(void)loadData
{
    [TYUtil updateUserPoint];
    [SVProgressHUD showWithStatus:@"加载中"];

    self.page=1;
    [_mTagLine getTishiStatesOfTheLineByTime:self.page bback:YES callback:^(NSArray *all, NSString *err) {
        self.tableview.tableFooterView.hidden = NO;

        if (all &&!err) {
            [SVProgressHUD dismiss];

            if(all.count<10)
            {
                self.refreshLabel.text = self.textNoMore;
            }
            else
            {
                self.refreshLabel.text = self.textPull;
            }
            self.page++;
            nowIsNew = NO;
            tempArray = [[NSMutableArray alloc]init];
            //   [tempArray removeAllObjects];
            [tempArray addObjectsFromArray:all];
            [self.tableview reloadData];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:@"加载失败"];
        }
    }];

}
- (void)viewDidLoad
{
    self.mPageName = @"线路详情";
    
    [super viewDidLoad];
    self.LeftBarTitle = @"返回";
    self.RightBarTitle = @"发布";
    tempArray = [[NSMutableArray alloc]init];
    
    [self loadData];
    // Do any additional setup after loading the view.
    self.haveFullupRefreshView = YES;
    [self initWithRefreshTableViewIos7:CGRectMake(0, 64, 320, DEVICE_Height-64) Ios6rect:CGRectMake(0, 44, 320, DEVICE_Height-64)];
    self.tableview.tableFooterView.hidden = YES;
    
    
    self.tableview.backgroundColor = ColorRGB(230, 230, 230);
    self.tableview.separatorStyle = UITableViewCellEditingStyleNone;
    
    //    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 48)];
    //    footerView.backgroundColor =[UIColor clearColor];
    //    self.tableview.tableFooterView = footerView;
    
    UIView *botBarView;
    if (SystemIsiOS7()) {
        botBarView  = [[UIView alloc]initWithFrame:CGRectMake(0, DEVICE_Height-48, 320, 48)];
        
    }
    else
    {
        botBarView  = [[UIView alloc]initWithFrame:CGRectMake(0, DEVICE_Height-68, 320, 48)];
    }
    UIImageView *botbg = [[UIImageView alloc]initWithFrame:botBarView.bounds];
    botbg.image = [UIImage imageNamed:@"dibubeijing.png"];
    [botBarView addSubview:botbg];
    
    
    float x = 0.0f;
    for (int i = 0; i<4; i++) {
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(x, 0, 80, 48)] ;
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(x+28, 3, 24, 24)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(x+28, 28, 30, 20)];
        btn.tag =10+i;
        image.tag = 20+i;
        label.tag = 30+i;
        
        [btn addTarget:self action:@selector(tabBarBtnTouched:) forControlEvents:UIControlEventTouchUpInside];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = ColorRGB(137, 137, 137);
        label.font = [UIFont boldSystemFontOfSize:12];
        if (i == 0) {
            image .image = [UIImage imageNamed:@"zuixin.png"];
            label.text = @"最新";
            image .image = [UIImage imageNamed:@"tishi_lan_2.png"];
            label.text = @"提示";
            
            label.textColor = [UIColor whiteColor];
            tempBtn = btn;
        }
        else if (i == 1)
        {
   
     
            
        }
        else if (i==2)
        {
            image .image = [UIImage imageNamed:@"jinghua.png"];
            label.text = @"精华";
        }
        else
        {
            image .image = [UIImage imageNamed:@"yantu.png"];
            label.text = @"沿途";
        }
        // [btn setBackgroundColor:[UIColor redColor]];
        [botBarView addSubview:btn];
        [botBarView addSubview:label];
        [botBarView addSubview:image];
        x+= 80;
    }
    
    // botBarView.backgroundColor = [UIColor blackColor];
 //   [self.view addSubview:botBarView];

}


-(void)tabBarBtnTouched:(UIButton *)sender
{
    if (tempBtn == sender ) {
        if (sender.tag == 13) {
           // YanTuVC *vc = [[YanTuVC alloc]initWithNibName:@"YanTuVC" bundle:nil];
          //  [self.navigationController pushViewController:vc animated:YES];
        }
        return;
    }
    int tag = sender.tag-10;

    if (tag == 0) {

        
    }
    else if (tag == 1)
    {
        DynamicDetailVC *vc = [[DynamicDetailVC alloc]init];
        vc.mTagLine = self.mTagLine;
        vc.lineTitle = self.lineTitle;
        NSMutableArray *array = [[NSMutableArray alloc]init];
        [array addObjectsFromArray: self.navigationController.viewControllers];
        
        [array removeLastObject];
        [array addObject:vc];
        [self.navigationController setViewControllers:array animated:NO];

        
        
        
    }
    else if (tag == 2)
    {
  
        JinghuaDynamicDetailVC *vc = [[JinghuaDynamicDetailVC alloc]init];
        vc.mTagLine = self.mTagLine;
        vc.lineTitle = self.lineTitle;
        NSMutableArray *array = [[NSMutableArray alloc]init];
        [array addObjectsFromArray: self.navigationController.viewControllers];
        
        [array removeLastObject];
        [array addObject:vc];
        [self.navigationController setViewControllers:array animated:NO];
        //精华
    }
    else if (tag == 3)
    {

        
    }
    
    
}
-(void)leftBtnTouched:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)rightBtnTouched:(id)sender{
    PublishVC *vc = [[PublishVC alloc]init];
    vc.comeFrom = 2;
    if ([TYUser isNeedLogin]) {
        LoginVC *logvc = [[LoginVC alloc]initWithNibName:@"LoginVC" bundle:nil];
        logvc.tagVC = vc;
        [self.navigationController pushViewController:logvc animated:YES];
        return;
        
    }
    [self.navigationController pushViewController:vc animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tempArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"DynamicDetailCell";
    
    DynamicDetailCell *cell = (DynamicDetailCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    
    BOOL isUsed = NO;
    if (!isUsed &&! cell) {
        UINib *nib = [UINib nibWithNibName:@"DynamicDetail" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:@"DynamicDetailCell"];
        // isUsed = YES;
        cell = (DynamicDetailCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
        
    }
    
    TYUserState* publish = [tempArray objectAtIndex:indexPath.row];
    
    //   [self.tableview reloadData];
    if (publish.mImgArr.count == 0 ||publish.mImgArr == nil) {
        for (int j =0; j<9; j++) {
            UIImageView *image = (UIImageView *)[cell viewWithTag:10+j];
            image.hidden = YES;
            
        }
    }
    else
    {
        for (int i = 0; i<publish.mImgArr.count; i++) {
            for (int j =0; j<9; j++) {
                UIImageView *image = (UIImageView *)[cell viewWithTag:10+j];
                
                if (j< publish.mImgArr.count) {
                    
                    image.hidden = NO;
                    //image.image = [publish.mImgArr objectAtIndex:j];
                    
                    NSString* ts = [publish.mImgArr objectAtIndex:j];
                    if( ts )
                    {
                        NSURL* turl  = [NSURL URLWithString:[Util makeSmallImgURL:ts]];
                        //NSLog(@"imgurl:%@",turl);
                        [image setImageWithURL: turl placeholderImage:S_D_IMG options:SDWebImageProgressiveDownload];
                        //   [image setImageWithURL:turl];
                        //[image setImageWithURL:turl placeholderImage:S_D_IMG];
                        
                        
                    }
                    else
                    {
                        image.image = S_D_IMG;
                    }
                }
                else
                {
                    image.hidden = YES;
                }
                //  image.image = [UIImage imageNamed:str];
                
            }
        }
    }
    
    [cell upDateCell:publish];
    
    cell.bgImage.layer.borderWidth = 0.5;
    cell.bgImage.layer.borderColor = [ColorRGB(212, 212, 212)CGColor];

    if ( publish.mTaguser.mSex == 2) {
        cell.sexImage.image = [UIImage imageNamed:@"men.png"];
    }
    else
    {
        cell.sexImage.image = [UIImage imageNamed:@"women.png"];
        
    }
    cell.AddrDistance.text = publish.mPBetweenInfo;
    cell.userAddr .text = publish.mPointInfo;
    if (publish.mPointInfo.length == 0) {
        cell.addrImgae.hidden = YES;
    }
    else
    {
        cell.addrImgae.hidden = NO;
    }
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:publish.mContext];
    NSInteger strLength = [publish.mContext length];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:2];
    [attString addAttribute:NSParagraphStyleAttributeName
                      value:style
                      range:NSMakeRange(0, strLength)];
    
    [cell.headImage setBackgroundImageWithURL:[NSURL URLWithString:publish.mTaguser.mHeadImgURL] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"70_70.png"]];
    [cell.topBtn addTarget:self action:@selector(headImageBtnTouched:) forControlEvents:UIControlEventTouchUpInside];
    cell.UserContent.attributedText = attString;
    
    cell.userPublishTime.text = [Util FormartTime:publish.mTimeC bfull:NO];
    cell.userName.text = publish.mTaguser.mUserDisplayName;
    cell.reBackCountLab.text =publish.mReBackCount>0? [NSString stringWithFormat:@"%d条回复",publish.mReBackCount]:@"暂无回复";
    
    cell.styleIconImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"style_icon_%d.png",publish.mStyle]];
    cell.nodeNameLab.text = publish.mRelInfo;
    // cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // cell.textLabel.text = @"success";
    return cell;
    
    
}
-(void)headImageBtnTouched:(UIButton *)sender
{
    DynamicDetailCell *cell = (DynamicDetailCell*)sender.superview.superview.superview.superview;
    NSIndexPath *indexpath = [self.tableview indexPathForCell:cell];
    
    TYUserState *comment = [tempArray objectAtIndex:indexpath.row];
    MyPublishVC *vc = [[MyPublishVC alloc]init];
    vc.fromMyself = NO;
    vc.user =comment.mTaguser;
    [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"%D",indexpath.row);
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DynamicReplyVC *vc = [[DynamicReplyVC alloc]init];
    vc.isFromHis = NO;
    vc.thePublish = [tempArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //    DynamicDetailVC *vc = [[DynamicDetailVC alloc]init];
    //    [self.navigationController pushViewController:vc animated:YES];
    
}
-(void)refresh
{
    
        [_mTagLine getTishiStatesOfTheLineByTime:self.page bback:NO callback:^(NSArray *all, NSString *err) {
            
            [self stopLoading];
            
            if (all &&!err) {
                nowIsNew = NO;
                //  [SVProgressHUD showSuccessWithStatus:@"加载成功"];
                // [tempArray removeAllObjects];
                [tempArray addObjectsFromArray:all];
                [self.tableview reloadData];
                if (all.count <ONE_PAGE) {
                    self.refreshLabel.text = self.textNoMore;
                //    [SVProgressHUD showErrorWithStatus:@"没有更多数据"];
                    self.page++;
                    
                    return ;
                }
                else
                {
                    self.refreshLabel.text = self.textPull;
                }
                self.page++;
                
            }
            else
            {
                [SVProgressHUD showErrorWithStatus:@"加载失败"];
            }
        }];
        
    
}

- (void)slimeRefreshStartRefresh:(SRRefreshView *)refreshView
{

        [TYUtil updateUserPoint];
        
        self.page=1;
        [_mTagLine getTishiStatesOfTheLineByTime:self.page bback:YES callback:^(NSArray *all, NSString *err) {
            [self.slimeView endRefresh];
            
            if (all &&!err) {
                [SVProgressHUD dismiss];
                self.page++;
                nowIsNew = NO;
                if(all.count<10)
                {
                    self.refreshLabel.text = self.textNoMore;
                }
                else
                {
                    self.refreshLabel.text = self.textPull;
                }

                tempArray = [[NSMutableArray alloc]init];
                //   [tempArray removeAllObjects];
                [tempArray addObjectsFromArray:all];
                [self.tableview reloadData];
            }
            else
            {
                [SVProgressHUD showErrorWithStatus:@"加载失败"];
            }
        }];
        
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
